package com.alibaba.user.controller;

import com.alibaba.basic.businessexception.BusinessException;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.user.dto.LoginDto;
import com.alibaba.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;
    // 用户登录
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }
    // 微信扫码登录
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody LoginDto loginDto){
        try {

            return logininfoService.wechatLogin(loginDto.getCode());
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {

            return logininfoService.wechatBinder(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }
}
